

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 获取所有域名后缀对应whois_server地址
*/
public class GetWhoisServerList {

    public static void main(String[] args) throws IOException {
       String iurl = "https://www.iana.org/domains/root/db";
        URL url = new URL(iurl);
        OkHttpClient client = new OkHttpClient.Builder().connectTimeout(600, TimeUnit.SECONDS).readTimeout(600, TimeUnit.SECONDS).build();

        try {
            Request request = new Request.Builder().url(iurl).build();
            Response response = client.newCall(request).execute();
            String html = response.body().string();

            Document doc = Jsoup.parse(html, "UTF-8");

        // 提取所有具有指定class的span标签
        Elements tldElements = doc.select("span.domain.tld");
        Pattern whoisPattern = Pattern.compile("<b>WHOIS Server:</b> (.*?)\n");

        BufferedWriter writer = new BufferedWriter(new FileWriter("D://suffixList.txt", true));
            // 遍历并打印顶级域名
        for (Element tldElement : tldElements) {
            String tld = tldElement.text();
            String d_suffix = tldElement.text();
            System.out.println(d_suffix);

            String n_suffix = d_suffix.split("\\.")[1];
            String new_url = iurl + '/' + n_suffix;

            try {
                Request newRequest = new Request.Builder().url(new_url).build();
                Response newResponse = client.newCall(newRequest).execute();
                String newHtml = newResponse.body().string();
                Document newDoc = Jsoup.parse(newHtml, "utf-8");

                Matcher matcher = whoisPattern.matcher(newHtml);
                if (matcher.find()) {
                    String server = matcher.group(1);
                    System.out.println(server);
                    writer.write(n_suffix + ":" + server + "\n");
                }
            } catch (Exception e) {
                System.out.println("超时");
            }
        }

        writer.close();
        System.out.println("抓取结束！！！");
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}
